/*
 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Anahide Tchertchian
 */
package org.nuxeo.theme.styling.service.registries;

import java.util.HashMap;
import java.util.Map;

import org.nuxeo.runtime.model.ContributionFragmentRegistry;
import org.nuxeo.theme.styling.service.descriptors.SimpleStyle;

/**
 * Registry for theme style resources, handling merge of registered {@link SimpleStyle} elements.
 *
 * @since 5.5
 */
public class StyleRegistry extends ContributionFragmentRegistry<SimpleStyle> {

    protected Map<String, SimpleStyle> themePageStyles = new HashMap<String, SimpleStyle>();

    @Override
    public String getContributionId(SimpleStyle contrib) {
        return contrib.getName();
    }

    @Override
    public void contributionUpdated(String id, SimpleStyle contrib, SimpleStyle newOrigContrib) {
        themePageStyles.put(id, contrib);
    }

    @Override
    public void contributionRemoved(String id, SimpleStyle origContrib) {
        themePageStyles.remove(id);
    }

    @Override
    public SimpleStyle clone(SimpleStyle orig) {
        SimpleStyle clone = new SimpleStyle();
        clone.setName(orig.getName());
        clone.setSrc(orig.getSrc());
        clone.setContent(orig.getContent());
        return clone;
    }

    @Override
    public void merge(SimpleStyle src, SimpleStyle dst) {
        // no merge => replace content
        dst.setSrc(src.getSrc());
        dst.setContent(src.getContent());
    }

    public SimpleStyle getStyle(String id) {
        return themePageStyles.get(id);
    }
}
